using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class OpGioControl : BUSControl
    {
        #region Local Variable
        private OpGioDataAccess _objDAO;
        #endregion Local Variable

        public OpGioControl(string type, string connectString, int timeout = 0)
            : base(type, connectString, timeout) { _objDAO = new OpGioDataAccess(Connnection); }

        public OpGioControl(BUSControl common)
            : base(common) { _objDAO = new OpGioDataAccess(common.Connnection); }

        #region Method
        public DataTable GetShortData(String XuongId, List<DTO.FilterParameter> filters, ref string sErr)
        {
            return _objDAO.GetShortData(XuongId, filters, ref sErr);
        }

        public DataTable GetByFilterToDataTable(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0)
        {
            return _objDAO.GetByFilterToDataTable(XuongId, filters, ref logMsg, indexPage, itemPerPage);
        }

        public int GetCountRecord(String XuongId, List<DTO.FilterParameter> filters, ref string sErr)
        {
            return _objDAO.GetCountRecord(XuongId, filters, ref sErr);
        }

        public OpGioInfo[] GetByFilter(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0)
        {
            OpGioInfo[] res = _objDAO.GetByFilter(XuongId, filters, ref logMsg, indexPage, itemPerPage);
            //get details

            return res;
        }

        public int Add(OpGioInfo obj, ref string sErr)
        {
            _objDAO.Add(obj, ref sErr);
            if (sErr == "")
            {
                //add details
            }
            return 1;
        }

        public string Update(OpGioInfo obj)
        {
            string sErr = _objDAO.Update(obj);
            // delete details
            // add details
            return sErr;
        }

        public string Delete(String XuongId, Int32 LineId)
        {
            string sErr = "";
            // delete details

            if (sErr == "") sErr = _objDAO.Delete(XuongId, LineId);
            return sErr;
        }
        public bool IsExist(String XuongId, Int32? LineId, ref string sErr)
        {
            return _objDAO.IsExist(XuongId, LineId, ref sErr);
        }

        public string InsertUpdate(OpGioInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.XuongId, obj.LineId, ref sErr)) { sErr = Update(obj); }
            else
            {
                if (sErr == "") Add(obj, ref sErr);
            }
            return sErr;
        }

        public string TransferIn(DataRow row)
        {
            OpGioInfo inf = new OpGioInfo(row);
            return InsertUpdate(inf);
        }

        public override string ToXmlString(DTOInfo[] lInf, ref string sErr)
        {
            string result = "";
            try
            {
                if (lInf != null && lInf.Length > 0)
                {
                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("OpGio");
                    doc.AppendChild(root);
                    foreach (DTO.DTOInfo x in lInf)
                    {
                        DTO.OpGioInfo it = x as DTO.OpGioInfo;
                        if (it == null) return "";

                        XmlElement row = doc.CreateElement("row");

                        if (it.LineId != null) row.SetAttribute("LineId", string.Format("{0}", it.LineId));
                        if (it.BatDau != null) row.SetAttribute("BatDau", string.Format("{0}", it.BatDau));
                        if (it.GioLam1 != null) row.SetAttribute("GioLam1", string.Format("{0}", it.GioLam1));
                        if (it.GioLam2 != null) row.SetAttribute("GioLam2", string.Format("{0}", it.GioLam2));
                        if (it.GioLam3 != null) row.SetAttribute("GioLam3", string.Format("{0}", it.GioLam3));
                        if (it.GioLam4 != null) row.SetAttribute("GioLam4", string.Format("{0}", it.GioLam4));
                        if (it.GioLam5 != null) row.SetAttribute("GioLam5", string.Format("{0}", it.GioLam5));
                        if (it.GioLam6 != null) row.SetAttribute("GioLam6", string.Format("{0}", it.GioLam6));
                        if (it.GioLam7 != null) row.SetAttribute("GioLam7", string.Format("{0}", it.GioLam7));
                        if (it.GioLam8 != null) row.SetAttribute("GioLam8", string.Format("{0}", it.GioLam8));
                        if (it.GioLam9 != null) row.SetAttribute("GioLam9", string.Format("{0}", it.GioLam9));
                        if (it.GioLam10 != null) row.SetAttribute("GioLam10", string.Format("{0}", it.GioLam10));
                        if (it.GioLam11 != null) row.SetAttribute("GioLam11", string.Format("{0}", it.GioLam11));
                        if (it.GioLam12 != null) row.SetAttribute("GioLam12", string.Format("{0}", it.GioLam12));
                        if (it.GioLam13 != null) row.SetAttribute("GioLam13", string.Format("{0}", it.GioLam13));
                        if (it.GioLam14 != null) row.SetAttribute("GioLam14", string.Format("{0}", it.GioLam14));
                        if (it.GioLam15 != null) row.SetAttribute("GioLam15", string.Format("{0}", it.GioLam15));
                        if (it.GioLam16 != null) row.SetAttribute("GioLam16", string.Format("{0}", it.GioLam16));
                        if (it.GioLam17 != null) row.SetAttribute("GioLam17", string.Format("{0}", it.GioLam17));
                        if (it.GioLam18 != null) row.SetAttribute("GioLam18", string.Format("{0}", it.GioLam18));
                        if (it.GioLam19 != null) row.SetAttribute("GioLam19", string.Format("{0}", it.GioLam19));
                        if (it.GioLam20 != null) row.SetAttribute("GioLam20", string.Format("{0}", it.GioLam20));
                        if (it.GioNghi1 != null) row.SetAttribute("GioNghi1", string.Format("{0}", it.GioNghi1));
                        if (it.GioNghi2 != null) row.SetAttribute("GioNghi2", string.Format("{0}", it.GioNghi2));
                        if (it.GioNghi3 != null) row.SetAttribute("GioNghi3", string.Format("{0}", it.GioNghi3));
                        if (it.GioNghi4 != null) row.SetAttribute("GioNghi4", string.Format("{0}", it.GioNghi4));
                        if (it.GioNghi5 != null) row.SetAttribute("GioNghi5", string.Format("{0}", it.GioNghi5));
                        if (it.GioNghi6 != null) row.SetAttribute("GioNghi6", string.Format("{0}", it.GioNghi6));
                        if (it.GioNghi7 != null) row.SetAttribute("GioNghi7", string.Format("{0}", it.GioNghi7));
                        if (it.GioNghi8 != null) row.SetAttribute("GioNghi8", string.Format("{0}", it.GioNghi8));
                        if (it.GioNghi9 != null) row.SetAttribute("GioNghi9", string.Format("{0}", it.GioNghi9));
                        if (it.GioNghi10 != null) row.SetAttribute("GioNghi10", string.Format("{0}", it.GioNghi10));
                        if (it.GioNghi11 != null) row.SetAttribute("GioNghi11", string.Format("{0}", it.GioNghi11));
                        if (it.GioNghi12 != null) row.SetAttribute("GioNghi12", string.Format("{0}", it.GioNghi12));
                        if (it.GioNghi13 != null) row.SetAttribute("GioNghi13", string.Format("{0}", it.GioNghi13));
                        if (it.GioNghi14 != null) row.SetAttribute("GioNghi14", string.Format("{0}", it.GioNghi14));
                        if (it.GioNghi15 != null) row.SetAttribute("GioNghi15", string.Format("{0}", it.GioNghi15));
                        if (it.GioNghi16 != null) row.SetAttribute("GioNghi16", string.Format("{0}", it.GioNghi16));
                        if (it.GioNghi17 != null) row.SetAttribute("GioNghi17", string.Format("{0}", it.GioNghi17));
                        if (it.GioNghi18 != null) row.SetAttribute("GioNghi18", string.Format("{0}", it.GioNghi18));
                        if (it.GioNghi19 != null) row.SetAttribute("GioNghi19", string.Format("{0}", it.GioNghi19));
                        if (it.GioNghi20 != null) row.SetAttribute("GioNghi20", string.Format("{0}", it.GioNghi20));
                        // details
                        //if (it.details != null) row.SetAttribute("details", JsonConvert.SerializeObject(it.details));

                        root.AppendChild(row);
                    }

                    result = doc.InnerXml;
                }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return result;
        }

        public override string TransferIn(DTOInfo inf, BUS.BUSControl.TransferMode mode)
        {
            string sErr = "";
            if (inf is OpGioInfo)
            {
                if (mode == BUS.BUSControl.TransferMode.AddNew) Add((DTO.OpGioInfo)inf, ref sErr);
                else if (mode == BUS.BUSControl.TransferMode.Update) sErr = Update((OpGioInfo)inf);
                else sErr = InsertUpdate((OpGioInfo)inf);
            }
            return sErr;
        }

        public override DTO.DTOInfo[] Preview(XmlDocument xml, ref string sErr, string db = null)
        {
            DTO.DTOInfo[] lRes = null;
            XmlElement doc = xml.DocumentElement;
            lRes = new DTOInfo[doc.ChildNodes.Count];

            for (int i = 0; i < doc.ChildNodes.Count; i++)
            {
                if (sErr != "") return lRes;
                try
                {
                    XmlElement ele = doc.ChildNodes[i] as XmlElement;

                    DTO.OpGioInfo inf = new DTO.OpGioInfo();

                    inf.XuongId = db;
                    if (GetStringAttribute(ele, "LineId") != "") inf.LineId = GetInt32Attribute(ele, "LineId");
                    if (GetStringAttribute(ele, "BatDau") != "") inf.BatDau = GetInt32Attribute(ele, "BatDau");
                    if (GetStringAttribute(ele, "GioLam1") != "") inf.GioLam1 = GetInt32Attribute(ele, "GioLam1");
                    if (GetStringAttribute(ele, "GioLam2") != "") inf.GioLam2 = GetInt32Attribute(ele, "GioLam2");
                    if (GetStringAttribute(ele, "GioLam3") != "") inf.GioLam3 = GetInt32Attribute(ele, "GioLam3");
                    if (GetStringAttribute(ele, "GioLam4") != "") inf.GioLam4 = GetInt32Attribute(ele, "GioLam4");
                    if (GetStringAttribute(ele, "GioLam5") != "") inf.GioLam5 = GetInt32Attribute(ele, "GioLam5");
                    if (GetStringAttribute(ele, "GioLam6") != "") inf.GioLam6 = GetInt32Attribute(ele, "GioLam6");
                    if (GetStringAttribute(ele, "GioLam7") != "") inf.GioLam7 = GetInt32Attribute(ele, "GioLam7");
                    if (GetStringAttribute(ele, "GioLam8") != "") inf.GioLam8 = GetInt32Attribute(ele, "GioLam8");
                    if (GetStringAttribute(ele, "GioLam9") != "") inf.GioLam9 = GetInt32Attribute(ele, "GioLam9");
                    if (GetStringAttribute(ele, "GioLam10") != "") inf.GioLam10 = GetInt32Attribute(ele, "GioLam10");
                    if (GetStringAttribute(ele, "GioLam11") != "") inf.GioLam11 = GetInt32Attribute(ele, "GioLam11");
                    if (GetStringAttribute(ele, "GioLam12") != "") inf.GioLam12 = GetInt32Attribute(ele, "GioLam12");
                    if (GetStringAttribute(ele, "GioLam13") != "") inf.GioLam13 = GetInt32Attribute(ele, "GioLam13");
                    if (GetStringAttribute(ele, "GioLam14") != "") inf.GioLam14 = GetInt32Attribute(ele, "GioLam14");
                    if (GetStringAttribute(ele, "GioLam15") != "") inf.GioLam15 = GetInt32Attribute(ele, "GioLam15");
                    if (GetStringAttribute(ele, "GioLam16") != "") inf.GioLam16 = GetInt32Attribute(ele, "GioLam16");
                    if (GetStringAttribute(ele, "GioLam17") != "") inf.GioLam17 = GetInt32Attribute(ele, "GioLam17");
                    if (GetStringAttribute(ele, "GioLam18") != "") inf.GioLam18 = GetInt32Attribute(ele, "GioLam18");
                    if (GetStringAttribute(ele, "GioLam19") != "") inf.GioLam19 = GetInt32Attribute(ele, "GioLam19");
                    if (GetStringAttribute(ele, "GioLam20") != "") inf.GioLam20 = GetInt32Attribute(ele, "GioLam20");
                    if (GetStringAttribute(ele, "GioNghi1") != "") inf.GioNghi1 = GetInt32Attribute(ele, "GioNghi1");
                    if (GetStringAttribute(ele, "GioNghi2") != "") inf.GioNghi2 = GetInt32Attribute(ele, "GioNghi2");
                    if (GetStringAttribute(ele, "GioNghi3") != "") inf.GioNghi3 = GetInt32Attribute(ele, "GioNghi3");
                    if (GetStringAttribute(ele, "GioNghi4") != "") inf.GioNghi4 = GetInt32Attribute(ele, "GioNghi4");
                    if (GetStringAttribute(ele, "GioNghi5") != "") inf.GioNghi5 = GetInt32Attribute(ele, "GioNghi5");
                    if (GetStringAttribute(ele, "GioNghi6") != "") inf.GioNghi6 = GetInt32Attribute(ele, "GioNghi6");
                    if (GetStringAttribute(ele, "GioNghi7") != "") inf.GioNghi7 = GetInt32Attribute(ele, "GioNghi7");
                    if (GetStringAttribute(ele, "GioNghi8") != "") inf.GioNghi8 = GetInt32Attribute(ele, "GioNghi8");
                    if (GetStringAttribute(ele, "GioNghi9") != "") inf.GioNghi9 = GetInt32Attribute(ele, "GioNghi9");
                    if (GetStringAttribute(ele, "GioNghi10") != "") inf.GioNghi10 = GetInt32Attribute(ele, "GioNghi10");
                    if (GetStringAttribute(ele, "GioNghi11") != "") inf.GioNghi11 = GetInt32Attribute(ele, "GioNghi11");
                    if (GetStringAttribute(ele, "GioNghi12") != "") inf.GioNghi12 = GetInt32Attribute(ele, "GioNghi12");
                    if (GetStringAttribute(ele, "GioNghi13") != "") inf.GioNghi13 = GetInt32Attribute(ele, "GioNghi13");
                    if (GetStringAttribute(ele, "GioNghi14") != "") inf.GioNghi14 = GetInt32Attribute(ele, "GioNghi14");
                    if (GetStringAttribute(ele, "GioNghi15") != "") inf.GioNghi15 = GetInt32Attribute(ele, "GioNghi15");
                    if (GetStringAttribute(ele, "GioNghi16") != "") inf.GioNghi16 = GetInt32Attribute(ele, "GioNghi16");
                    if (GetStringAttribute(ele, "GioNghi17") != "") inf.GioNghi17 = GetInt32Attribute(ele, "GioNghi17");
                    if (GetStringAttribute(ele, "GioNghi18") != "") inf.GioNghi18 = GetInt32Attribute(ele, "GioNghi18");
                    if (GetStringAttribute(ele, "GioNghi19") != "") inf.GioNghi19 = GetInt32Attribute(ele, "GioNghi19");
                    if (GetStringAttribute(ele, "GioNghi20") != "") inf.GioNghi20 = GetInt32Attribute(ele, "GioNghi20");
                    // details
                    //string details = GetStringAttribute(ele, "details");
                    //inf.details = JsonConvert.DeserializeObject<CsDataAccessGroupInfo.RoleDetail[]>(details);
                    lRes[i] = inf;
                }
                catch (Exception ex) { sErr = ex.Message; }
            }
            return lRes;
        }
        public bool validate(JObject o, bool pkOnly, ref string sErr)
        {
            bool inputErr = false;
            if (pkOnly)
            {
                if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
            }
            else
            {
                if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
                if (o["BatDau"] != null) { }//Check validate 
                if (o["GioLam1"] != null) { }//Check validate 
                if (o["GioLam2"] != null) { }//Check validate 
                if (o["GioLam3"] != null) { }//Check validate 
                if (o["GioLam4"] != null) { }//Check validate 
                if (o["GioLam5"] != null) { }//Check validate 
                if (o["GioLam6"] != null) { }//Check validate 
                if (o["GioLam7"] != null) { }//Check validate 
                if (o["GioLam8"] != null) { }//Check validate 
                if (o["GioLam9"] != null) { }//Check validate 
                if (o["GioLam10"] != null) { }//Check validate 
                if (o["GioLam11"] != null) { }//Check validate 
                if (o["GioLam12"] != null) { }//Check validate 
                if (o["GioLam13"] != null) { }//Check validate 
                if (o["GioLam14"] != null) { }//Check validate 
                if (o["GioLam15"] != null) { }//Check validate 
                if (o["GioLam16"] != null) { }//Check validate 
                if (o["GioLam17"] != null) { }//Check validate 
                if (o["GioLam18"] != null) { }//Check validate 
                if (o["GioLam19"] != null) { }//Check validate 
                if (o["GioLam20"] != null) { }//Check validate 
                if (o["GioNghi1"] != null) { }//Check validate 
                if (o["GioNghi2"] != null) { }//Check validate 
                if (o["GioNghi3"] != null) { }//Check validate 
                if (o["GioNghi4"] != null) { }//Check validate 
                if (o["GioNghi5"] != null) { }//Check validate 
                if (o["GioNghi6"] != null) { }//Check validate 
                if (o["GioNghi7"] != null) { }//Check validate 
                if (o["GioNghi8"] != null) { }//Check validate 
                if (o["GioNghi9"] != null) { }//Check validate 
                if (o["GioNghi10"] != null) { }//Check validate 
                if (o["GioNghi11"] != null) { }//Check validate 
                if (o["GioNghi12"] != null) { }//Check validate 
                if (o["GioNghi13"] != null) { }//Check validate 
                if (o["GioNghi14"] != null) { }//Check validate 
                if (o["GioNghi15"] != null) { }//Check validate 
                if (o["GioNghi16"] != null) { }//Check validate 
                if (o["GioNghi17"] != null) { }//Check validate 
                if (o["GioNghi18"] != null) { }//Check validate 
                if (o["GioNghi19"] != null) { }//Check validate 
                if (o["GioNghi20"] != null) { }//Check validate 
            }
            return inputErr;
        }
        public override bool isField(string field)
        {
            return new DTO.OpGioInfo().isField(field);
        }
        #endregion Method

        public int GetTimePos(DTO.OpGioInfo g, DateTime d)
        {
            int res = 0;
            DataRow r = g.ToDataRow(g.ToDataTable());
            int now = d.Hour * 60 + d.Minute;
            res = FindTime(now, r, 1, 20);
            return res;
        }

        private int FindTime(int now, DataRow r, int p1, int p2)
        {
            int time = 0;
            for (int i = 1; i <= 20; i++)
            {
                time += Convert.ToInt32(r["GioLam" + i]);
                if (now < time)
                {
                    return i;
                }
                else if (now < time + Convert.ToInt32(r["GioNghi" + i]))
                {
                    return 0;
                }
            }
            return 0;
        }

    }
}
